import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseGuards,
  Query,
} from '@nestjs/common';
import { UsersService } from './users.service';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { User } from '@libs/db/models/user.model';
import { AuthGuard } from '@nestjs/passport';

@UseGuards(AuthGuard('jwt'))
@ApiBearerAuth()
@ApiTags('用户')
@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @ApiOperation({ summary: '创建用户' })
  @Post()
  create(@Body() user: User) {
    return this.usersService.create(user);
  }

  @ApiOperation({ summary: '用户列表' })
  @Get()
  findAll(@Query() query: any) {
    return this.usersService.findAll(query);
  }

  @ApiOperation({ summary: '查询用户' })
  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.usersService.findOne(id);
  }

  @ApiOperation({ summary: '修改用户' })
  @Patch(':id')
  update(@Param('id') id: string, @Body() user: User) {
    return this.usersService.update(id, user);
  }

  @ApiOperation({ summary: '删除用户' })
  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.usersService.remove(id);
  }

  @ApiOperation({ summary: '用户配置' })
  @Post('options')
  userConfigs(): any {
    return {
      title: '用户列表',
      descriptions: '显示和处理用户列表',
      items: [
        {
          label: '用户名',
          prop: 'username',
          type: 1,
        },
        {
          label: '密码',
          prop: 'password',
          type: 1,
          display: true,
        },
        {
          label: '电话',
          prop: 'tel',
          type: 1,
        },
        {
          label: '地址',
          prop: 'address',
          type: 1,
        },
        {
          label: '权限',
          prop: 'grant',
          type: 2,
          keyName: 'name',
          items: [
            {
              name: '用户',
              _id: 0,
            },
            {
              name: '管理员',
              _id: 1,
            },
          ],
        },
      ],
    };
  }
}
